/*
* @Author: JINPENG
* @Date:   2018-05-28 22:12:28
* @Last Modified by:   JINPENG
* @Last Modified time: 2018-09-06 21:15:25
*/
'use strict';
require('./index.css');
require('page/common/nav-simple/index.js');
var _user   = require('service/user-service.js');
var _mm = require('util/mm.js');
// 表单里的错误提示
var formError = {
    show : function(errMsg){
        $('.error-item').show().find('.err-msg').text(errMsg);
    },
    hide : function(){
        $('.error-item').hide().find('.err-msg').text('');
    }
};
var page = {
	init : function(){
		this.bindEvent();
	},
	bindEvent : function(){
		var _this = this;
		$('#submit').click(function(){
			_this.submit();
		});
		$('#user-content').click(function(e){
			if (e.keyCode === 13) {
				_this.submit();
			}
		});
	},
	submit : function(){
		var formData = {
			username : $.trim($('#username').val()),
			password : $.trim($('#password').val()),
		},
		validateResult = this.formVslidate(formData);
		if (validateResult.status) {
			_user.login(formData,function(res){
				window.location.href = _mm.getUrlParam('redirect') || './index.html';
			},function(errMsg){
				formError.show(errMsg);
			});
		}
		else{
			formError.show(validateResult.msg);
		}
	},
	formVslidate : function(formData){
		var result = {
			status : false,
			msg    : ''
		};
		if (!_mm.validate(formData.username,'require')) {
			result.msg = '用户名不能为空';
			return result;
		}
		if (!_mm.validate(formData.password,'require')) {
			alert(formData.password);
			result.msg = '密码不能为空';
			return result;
		}
		result.status = true;
		result.msg    = '验证通过';
		return result;

	}
};
$(function(){
	page.init();
});